<?php
global $_RQ,$_AR;
$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';
$merchid=$this->checklogin();
if($_RQ['op']=='list'){
	$pindex = max(1, intval($_SESSION['page']));
	$psize = 15;
	$params=array('uniacid'=>$_SESSION['uniacid'],'merchid'=>$merchid);
    $orderby='id desc';
	$total='';
	$return['list']=pdo_getslice('jy_weishop_printer_template',$params,array($pindex,$psize),$total,$fields,'',$orderby);
	
	$htmls=array(
			'type'=>'list',
			'tabs'=>array(
				array('op'=>'list','title'=>'打印模板管理'),
				array('op'=>'edit','title'=>'添加模板'),
			),
			'list'=>array('data'=>'list','list'=>array(
				array('name'=>'title','title'=>'模板名称','type'=>'string'),
				
				
			),
			),
		);
	$return['htmls']=$htmls;	
	exi($return);
}
if($_RQ['op']=='edit'){
	$datademo=model('jy_weishop/printtpl')->datademo();
	$datademo1=model('jy_weishop/printtpl')->datademo1();
	$datademo2=model('jy_weishop/printtpl')->datademo2();
	$datademo3=model('jy_weishop/printtpl')->datademo3();
	if($_RQ['id']){
		$item=pdo_get('jy_weishop_printer_template',array('id'=>$_RQ['id']));	
		$item['print_data']=json_decode($item['print_data'],true);
		$item['print_data']=$item['print_data'][0]['value']?$item['print_data']:$return['datademo'];
	}else{
		$item=array('print_data'=>$datademo);
	}
	$return['item']=$item;
	$datatypes=array(
		array('type'=>'toplogo','title'=>'LOGO图片','value'=>''),
		array('type'=>'text','title'=>'文本内容','value'=>'填写文本内容'),
		array('type'=>'goodsinfo','title'=>'商品内容','value'=>'无需设置'),
		array('type'=>'ewm','title'=>'二维码','value'=>custom_url('jy_weishop/index/',true)),
		array('type'=>'code','title'=>'条形码','value'=>'[订单号]'),
	);
	$return['index']=0;
	$return['editdata']=$item['print_data'][0];
	$return['datatypes']=$datatypes;
	$return['textstytle']=array('居左','居中','居右');
	$return['bold']=array('默认','加粗');
	$return['fontstytle']=array('默认','S1号','S2号','S3号','S4号','S5号','S6号','S7号','S8号');	
	$return['datademos']=array(
		array('data'=>$datademo,'title'=>'常规全单打印','value'=>''),
		array('data'=>$datademo1,'title'=>'单商品打印','value'=>''),
		array('data'=>$datademo2,'title'=>'单商品小标签打印','value'=>''),
		array('data'=>$datademo3,'title'=>'单商品大标签打印','value'=>''),
	);
	$return['msgtpl_tags']=model('jy_weishop/model')->msgtpl_tags();
	exi($return);
	}
if($_RQ['op']=='post'){
	$postdata=get_postdata();
	$data=array_fields(array('title','iscode','qrcode','isqrcode','print_style','print_top','goodssn','print_data'),$postdata);
	
	$data['print_data']=ijson_encode($data['print_data']);
	
	$id=intval($postdata['id']);
			if($id){
				pdo_update('jy_weishop_printer_template',$data,array('id'=>$id));
				exi('设置成功！','','list');
			}else{
			    $data['uniacid']=$_SESSION['uniacid'];
				 $data['merchid']=$merchid;
				pdo_insert('jy_weishop_printer_template',$data);
				exi('添加成功！','','list');
			}
			}
if($_RQ['op']=='del'){

	$id=intval($_RQ['id']);
	if($id){
		pdo_delete('jy_weishop_printer_template',array('uniacid'=>$_SESSION['uniacid'],'merchid'=>$merchid,'id'=>$id));	
	}
	$del=trim($_RQ['del'],';');
	if($del){
		$del=explode(';',$del);
		foreach($del as $id){
			$id=intval($id);
			if($id){
				pdo_delete('jy_weishop_printer_template',array('uniacid'=>$_SESSION['uniacid'],'merchid'=>$merchid,'id'=>$id));
			}
		}
	}
	exi('删除成功！');
}
?>